<?php

namespace App\Repository;

use App\Entity\AdminModule;
use App\Utils\Enum\AdminModuleEnum;
use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository;
use Doctrine\Persistence\ManagerRegistry;

/**
 * @method AdminModule|null find($id, $lockMode = null, $lockVersion = null)
 * @method AdminModule|null findOneBy(array $criteria, array $orderBy = null)
 * @method AdminModule[]    findAll()
 * @method AdminModule[]    findBy(array $criteria, array $orderBy = null, $limit = null, $offset = null)
 */
class AdminModuleRepository extends ServiceEntityRepository {
    public function __construct (ManagerRegistry $registry) {
        parent::__construct($registry, AdminModule::class);
    }

    /**
     * User: Gao
     * Date: 2020/3/21
     * Description: 获取指定模块集合
     * @param array $ids
     * @param bool $is_menu
     * @param bool $is_source_module
     * @return array
     */
    public function getModules ($ids = [], $is_menu = true, $is_source_module = true) {
        $result = [];

        $query = $this->createQueryBuilder('a')
            ->select('a.id', 'a.pid', 'a.name', 'a.sort')
            ->where('a.status = :status')
            ->orderBy('a.pid', 'ASC')
            ->setParameter('status', true);
        if ($is_menu) {
            $query->andWhere('a.type = :type')->setParameter('type', AdminModuleEnum::TYPE_MENU);
        }
        if (count($ids) > 0 || $is_source_module) {
            $query->andWhere('a.id IN (:ids)')->setParameter('ids', $ids);
        }
        $objs = $query->getQuery()->getArrayResult();

        // 整合成id为键的数组
        $items = [];
        foreach ($objs as $module) {
            $items[$module['id']] = $module;
        }

        // 通过引用实现无限分类
        foreach ($items as $key => $module) {
            if (isset($items[$module['pid']])) {
                $items[$module['pid']]['children'][] = &$items[$key];
            } else {
                $items[$key]['children'] = [];
                $result[$items[$key]['sort']] = &$items[$key];
            }
        }

        // children排序处理
        foreach ($result as &$module) {
            $sort_array = [];
            foreach ($module['children'] as $children) {
                $sort_array[$children['sort']] = $children;
            }
            ksort($sort_array);
            $module['children'] = array_merge($sort_array);
        }

        ksort($result);

        return array_merge($result);
    }

    /**
     * 获取用户组关联的模块集合
     * @param array $ids
     * @return array|mixed
     * @throws \Exception
     */
    public function getAuthSigns ($ids = []) {
        $result = [];
        $items = $this->createQueryBuilder('a')
            ->select('a.auth_sign')
            ->where('a.status = :status')
            ->andWhere('a.id in (:ids)')
            ->setParameter('status', true)
            ->setParameter('ids', $ids)
            ->getQuery()
            ->getArrayResult();
        foreach ($items as $item) {
            array_push($result, $item['auth_sign']);
        }
        return $result;
    }
}
